﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WFNetLib.GSMModem;

namespace 自消费
{
    public partial class TaskThread
    {
        bool InitWapTask()
        {
            try
            {
                Com.ReadTimeout = 1000;
                ATCommand atComm = new ATCommand(Com);
                OnAddLog("初始化设备", (int)TaskThreadEventType.Log);
                if (!atComm.SendAT("ATE0"))
                {
                    OnAddLog("初始化设备失败", (int)TaskThreadEventType.Error);
                    return false;
                }
                if (!atComm.SendAT("AT+CREG?"))//查询是否已经注册上网络
                {
                    OnAddLog("注册网络未知", (int)TaskThreadEventType.Error);
                    return false;
                }
                int stat = 1;
                stat = Convert.ToInt32(atComm.ATResult.Substring(atComm.ATResult.IndexOf(',') + 1));
                if (stat != 1 && stat != 5)
                {
                    OnAddLog("无法注册上网络:" + stat.ToString(), (int)TaskThreadEventType.Error);
                    return false;
                }
                if (!atComm.SendAT("AT+CGDCONT=1,\"IP\",\"CMWAP\""))
                {
                    OnAddLog("无法注册上网络", (int)TaskThreadEventType.Error);
                    return false;
                }
                Com.ReadTimeout = 5000;
                if (!atComm.SendAT("AT+CGATT=1"))
                {
                    atComm.SendAT("AT+CGATT=0");
                    return false;
                }
//                 if (!atComm.SendAT("AT+CGATT=1"))
//                 {
//                     OnAddLog("无法使用GPRS", (int)TaskThreadEventType.Error);
//                     return false;
//                 } 
                return true;
            }
            catch (InvalidOperationException ex)//端口被关闭,任务结束
            {
                throw ex;
            }
            catch (System.Exception ex)
            {
                return false;
            }
            finally
            {
                
            }
        }
    }
}
